(function() {
    var currentIndex = 0;
    var slides = document.querySelectorAll('.slick li');
    var dots = document.querySelectorAll('.dots li');
    var slider = document.querySelector('.slick');
    var prevButton = document.querySelector('.banner .prev');
    var nextButton = document.querySelector('.banner .next');
    var intervalId;
  
    // 复制第一张图片并添加到最后
    var firstSlide = slides[0].cloneNode(true);
    slider.appendChild(firstSlide);
    var newSlides = document.querySelectorAll('.slick li'); // 更新slides集合
    var newDots = document.querySelectorAll('.dots li'); // 更新dots集合
  
    function showSlide(index) {
        // 更新小点的激活状态
        newDots.forEach((dot, i) => {
            // 确保我们是在操作按钮元素
            var dotButton = dot.querySelector('button');
            dotButton.classList.toggle('dots_active', i === (index % slides.length));
        });
  
        // 设置slick容器的transform属性，实现无缝滚动
        // 设置过渡效果
        slider.style.transition = `transform 0.5s ease`;
        slider.style.transform = `translateX(-${index * 100}%)`;
        currentIndex = index;
  
        if (index >= slides.length) {
            // 等待1秒后瞬间跳转到真正的第一张
            setTimeout(function() {
                slider.style.transition = 'none'; // 禁用过渡效果
                slider.style.transform = `translateX(0)`; // 瞬间移动到第一张
                currentIndex = 0; // 更新currentIndex为0
            }, 1000); // 1秒后执行
        }
    }
  
    function startAutoPlay() {
        // 开始自动播放
        intervalId = setInterval(function() {
            currentIndex = (currentIndex + 1) % newSlides.length;
            showSlide(currentIndex);
        }, 3000); // 3秒切换一次
    }
  
    // 鼠标悬停时停止自动播放
    document.querySelector('.banner').addEventListener('mouseover', function() {
        clearInterval(intervalId);
    });
  
    // 鼠标离开时恢复自动播放
    document.querySelector('.banner').addEventListener('mouseout', function() {
        startAutoPlay();
    });
  
    // 点击小点切换幻灯片
    newDots.forEach((dot, index) => {
        dot.addEventListener('click', function() {
            showSlide(index);
        });
    });
  
    // 点击上一张按钮
    prevButton.addEventListener('click', function() {
        showSlide((currentIndex - 1 + newSlides.length) % newSlides.length);
    });
  
    // 点击下一张按钮
    nextButton.addEventListener('click', function() {
        showSlide((currentIndex + 1) % newSlides.length);
    });
  
    // 初始化轮播图
    startAutoPlay();
    showSlide(currentIndex);
  })();